package com.vil.controller;

import javax.servlet.http.HttpSession;
import com.vil.common.Constants;
import com.vil.common.ServerResponse;
import com.vil.entity.Admin;
import com.vil.entity.Village;
import com.vil.service.VillageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("village")
public class VillageController extends BaseController{


    @Autowired
    private VillageService villageService;
    //去小区页面
	@RequestMapping("villageList.html")
	public String pVillageList(Model model, HttpSession session) {
		Admin currentAdmin = (Admin) session
				.getAttribute(Constants.USER_SESSION);
		if (currentAdmin == null) {
			return "admin/login";
		}
		ServerResponse serverResponse = villageService.getListByManyConditions(	1, 5, null);
		model.addAttribute("eq", serverResponse.getData());
		return "admin/communitylist";
	}

    //获取小区列表
    @PostMapping("viewvillagelist")
    @ResponseBody
    public ServerResponse viewvillagelist(@RequestParam(value = "condition",defaultValue = "",required = false) String condition,
                                            @RequestParam(value = "condition1",defaultValue = "",required = false) String condition1,
                                            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                            @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
                                            Model model){
    	Village village = new Village();
    	village.setNumber(condition);
        village.setName(condition1);
        return villageService.getListByManyConditions(pageIndex,pageSize,village);

    }

    //去新增页面
    @GetMapping("communityadd.html")
    public String communityadd(){
        return "admin/communityadd";
    }

    //新增小区
    @PostMapping("addCommunitySelective")
    public ServerResponse addDeviceSelective(Village village){
       if (this.isActive()==false){
           return ServerResponse.buildErrorMsg("您的账号未被激活，不能进行该操作");
       }
       return villageService.insertSelective(village);
    }

    //去编辑页面
    @GetMapping("community_edit.html/{id}")
    public String community_edit(@PathVariable Long id,Model model){
        ServerResponse byId = villageService.getById(id);
        model.addAttribute("e",byId.getData());
        return "admin/community_edit";
    }

    //修改
    @PostMapping("editCommunity")
    @ResponseBody
    public ServerResponse editCommunitySelective(Village village){
        if (this.isActive()==false){
            return ServerResponse.buildErrorMsg("您的账号未被激活，不能进行该操作");
        }
        return villageService.updateByPrimaryKeySelective(village);
    }

}
